﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ProgNetDxComponents.ExtenderProviders.Browsers;
using ProgNetDxComponents.ExtenderProviders.Language;
using MiniKsiegowosc.DbUpdate;
using ProgNetComponents.Ini;
using ProgNetDxComponents.ExtenderProviders.Layout;
using ProgNetComponents.Data.SqlCe;
using ProgNetDxComponents.ExtenderProviders.Options;

namespace MiniKsiegowosc
{
    static class Program
    {
        private static IniFile _IniFile = new IniFile(Application.StartupPath + System.IO.Path.DirectorySeparatorChar + Application.ProductName + ".ini");
        public static IniFile IniFile
        {
            get { return _IniFile; }
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ProgNetLanguage.LogMissingValues = true;
            IniFile.ReadIni();

            string Connection = IniFile.GetValueFormCache("MAIN", "CONNECTION");
            if (Connection.IsNull())
            {
                Connection = @"Data Source=..\..\MiniKsiegowosc;Encrypt Database=False;Max Database Size = 4091";
                IniFile.WriteValueToCache("MAIN", "CONNECTION", Connection);
                IniFile.SaveIni();
            }
            
            SqlCeDataManager.DefaultAutoLogExceptions = true;

            ProgNetDxComponents.ExtenderProviders.Options.DBOptionGrabber g = new ProgNetDxComponents.ExtenderProviders.Options.DBOptionGrabber();
            SqlCeDataManager optionManager = new SqlCeDataManager();
            optionManager.AutoLogExceptions =  ProgNetComponents.ThreeStateBoolean.True;
            g.DataManager = optionManager;
            g.NameColumn = "STP_OP_NAZWA";
            g.ValueColumn = "STP_OP_WARTOSC";
            g.TableName = "STP_OPCJE";

            ProgNetDxComponents.ExtenderProviders.Options.OptionCache.Grabber = g;
            ProgNetComponents.Data.SqlCe.SqlCeDataManager.GlobalConnectionString = Connection;
            LayoutControlerDB l = new LayoutControlerDB();
            l.Manager = new SqlCeDataManager();
            l.Manager.AutoLogExceptions = ProgNetComponents.ThreeStateBoolean.True;
            l.LayoutTableName = "STP_LAYOUT";
            l.LayoutNameColumn = "STP_LAY_NAZWA";
            l.LayoutXmlColumn = "STP_LAY_XML";
            l.ConvertToByteArray = true;
            ProgNetDxComponents.ExtenderProviders.Layout.LayoutControllerBase.Instance = l;

            new DbUpdateClass().UpdateAll();

            ProgNetLanguage pl = new ProgNetLanguage(new System.Globalization.CultureInfo("pl-PL"));
            pl.RegisterManager("DB", typeof(Lng.db));
            ProgNetLanguage.CurrentLanguageCode = pl.Code;

            
            ProgNetComponents.Data.Query.SelectDataCache.DataManagerType = typeof(ProgNetComponents.Data.SqlCe.SqlCeDataManager);
            
            BrowserCache.DataGrabber = new BrowserDataGrabberSqlCe("STP_BROWSER_STATE", "STP_BRS_TABELA", "STP_BRS_DATA");
            BrowserCache.ConfigReader = new BrowserConfigReaderSqlCe(BrowserCache.DataGrabber, "STP_BROWSER_CONFIG", "STP_BC_NAZWA", "STP_BC_DISPLAY", "STP_BC_MIN");

            new Initialization.InitControl();
            OptionCache.Grabber.GetAllOptions();
            Application.Run(new MiniKsiegowoscMain());
        }
    }
}
